package com.ruoyi.standingbook.greenery.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.standingbook.greenery.domain.bo.GreeneryImportBaseBo;
import com.ruoyi.standingbook.greenery.domain.vo.GreeneryImportBaseVo;
import com.ruoyi.standingbook.greenery.domain.GreeneryImportBase;
import com.ruoyi.standingbook.greenery.mapper.GreeneryImportBaseMapper;
import com.ruoyi.standingbook.greenery.service.IGreeneryImportBaseService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 道路绿化管养导入Service业务层处理
 *
 * @author cc
 * @date 2023-08-29
 */
@RequiredArgsConstructor
@Service
public class GreeneryImportBaseServiceImpl implements IGreeneryImportBaseService {

    private final GreeneryImportBaseMapper baseMapper;

    /**
     * 查询道路绿化管养导入
     */
    @Override
    public GreeneryImportBaseVo queryById(String batchNo){
        return baseMapper.selectVoById(batchNo);
    }

    /**
     * 查询道路绿化管养导入列表
     */
    @Override
    public TableDataInfo<GreeneryImportBaseVo> queryPageList(GreeneryImportBaseBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<GreeneryImportBase> lqw = buildQueryWrapper(bo);
        Page<GreeneryImportBaseVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询道路绿化管养导入列表
     */
    @Override
    public List<GreeneryImportBaseVo> queryList(GreeneryImportBaseBo bo) {
        LambdaQueryWrapper<GreeneryImportBase> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<GreeneryImportBase> buildQueryWrapper(GreeneryImportBaseBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<GreeneryImportBase> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getProcessStatus() != null, GreeneryImportBase::getProcessStatus, bo.getProcessStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getProcessMsg()), GreeneryImportBase::getProcessMsg, bo.getProcessMsg());
        lqw.eq(bo.getAttachMsg() != null, GreeneryImportBase::getAttachMsg, bo.getAttachMsg());
        lqw.eq(StringUtils.isNotBlank(bo.getExcelUrl()), GreeneryImportBase::getExcelUrl, bo.getExcelUrl());
        return lqw;
    }

    /**
     * 新增道路绿化管养导入
     */
    @Override
    public Boolean insertByBo(GreeneryImportBaseBo bo) {
        GreeneryImportBase add = BeanUtil.toBean(bo, GreeneryImportBase.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setBatchNo(add.getBatchNo());
        }
        return flag;
    }

    /**
     * 修改道路绿化管养导入
     */
    @Override
    public Boolean updateByBo(GreeneryImportBaseBo bo) {
        GreeneryImportBase update = BeanUtil.toBean(bo, GreeneryImportBase.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(GreeneryImportBase entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除道路绿化管养导入
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
